package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.apple.util.RandomUtility;
import yxy.cherry.data.bean.actfloprandom;
import yxy.cherry.data.bean.actfloptarget;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopCard;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 翻牌 - 抽奖
 *
 * @author Dell
 */
@MessageType(proto = Command.ActFlopCardLuckDraw, classtype = ActiveMessage.ActFlopCardLuckDrawRequest.class)
public class CMD_Active_翻牌_抽奖_Flop extends Command<ActiveMessage.ActFlopCardLuckDrawRequest> {

    private static final Logger logger = Logger.create(CMD_Active_翻牌_抽奖_Flop.class);

    private static final int[] precent = {10, 20, 40, 80, 90, 150, 180, 300, 450, 600, 750, 900, 1500, 2000, 2500, 2650, 2800, 2950, 3100, 3250, 3400, 3550, 3700, 3850, 4000, 4150, 4300, 4450, 4600, 10000};

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardLuckDrawRequest request) throws Exception {
        String playercode = messageSource.getTag();
        int position = request.getPosition();
        ActiveMessage.ActFlopCardLuckDrawResponse.Builder response = ActiveMessage.ActFlopCardLuckDrawResponse.newBuilder();

        ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if (!am.running()) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if (!am.getFcmap().containsKey(position)) {
            send(messageSource, response, Result.DataError);
            return;
        }


        //todo ---------------------获取对象参数 根据概率修改翻牌规则--------------------
        //大奖位置
        int grand = 0;
        //未抽中的位置
        ArrayList<Integer> noPullList = new ArrayList<>();
        //抽过的位置
        ArrayList<Integer> hasPullList = new ArrayList<>();

        for (FlopCard card : am.getFcmap().values()) {
            if (!card.isFlop()) {
                noPullList.add(card.getPosition());
            }

            if (card.isFlop()) {
                hasPullList.add(card.getPosition());
            }

            if (card.isGrand()) {
                grand = card.getPosition();
            }
        }
        //大奖是否已经翻过
        Boolean isGetGrand = hasPullList.contains(grand) ? true : false;
        //已经翻过的次数
        int hasPullNum = hasPullList.size();
        //todo ---------------------获取对象参数 根据概率修改翻牌规则--------------------


        FlopCard fc = am.getFcmap().get(position);
        if (fc.isFlop()) {
            send(messageSource, response, Result.ActHadGet);
            return;
        }

        Currency costcur = Currency.create(CurrencyType.CT_物品, 1038, 1);
        boolean canbuy = new CurrencyHandle().costCheck(playercode, costcur);
        if (!canbuy) {
            send(messageSource, response, new Result(Result.CostFail, CurrencyType.CT_物品.getNumber(), 1038));
            return;
        }

        new CurrencyHandle().cost(playercode, CCReason.活动_翻牌, costcur);
        fc.setFlop(true);

        Currency cur;

        //本次抽奖的概率
        System.out.println(precent);
        System.out.println(precent[28]);
        int probability = precent[hasPullNum];
        int random = RandomUtility.nextInt(0, 10000);

        //本次是大奖但概率没中，把大奖和没抽的奖品更换
        if (random > probability && grand == position && noPullList.size() != 1) {
            //本次:fc    大奖:am.getFcmap().get(grand)

            //没有抽的位置要去掉自己当前这次
            Iterator<Integer> iterator = noPullList.iterator();
            while (iterator.hasNext()) {
                if (iterator.next() == position) {
                    iterator.remove();
                    break;
                }
            }

            //取没抽的某次
            FlopCard otherCard = am.getFcmap().get(noPullList.get(0));
            int otherCardId = otherCard.getId();
            int fcId = fc.getId();
            fc.setId(otherCardId);
            fc.setGrand(false);
            otherCard.setId(fcId);
            otherCard.setGrand(true);
        }

        //本次不是大奖，但概率中了，把大奖和没抽的奖品更换
        if (random < probability && grand != position && !isGetGrand) {
            //本次:fc    大奖:am.getFcmap().get(grand)
            FlopCard grandCard = am.getFcmap().get(grand);
            int grandCardId = grandCard.getId();
            int fcId = fc.getId();
            fc.setId(grandCardId);
            fc.setGrand(true);
            grandCard.setId(fcId);
            grandCard.setGrand(false);
        }

        am.set();

        //如果中奖
        if (fc.isGrand()) { //是大奖
            actfloptarget data = ActDataCenter.getData(am.getSelectid(), actfloptarget.class, playercode);
            cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
        } else {
            actfloprandom data = ActDataCenter.getData(fc.getId(), actfloprandom.class, playercode);
            cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
        }

        PackageMessage.Item item = new CurrencyHandle().packItem(cur);
        response.setCur(item);
        response.setIsgrand(fc.isGrand());

        new CurrencyHandle().addCurrency(playercode, CCReason.活动_翻牌, cur);

        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActFlopCardLuckDrawResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
